package com.bookstore.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bookstore.entity.Category;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
    
    /**
     * 获取子分类列表
     * @param parentId 父分类ID
     * @return 子分类列表
     */
    @Select("SELECT * FROM category WHERE parent_id = #{parentId} ORDER BY sort")
    List<Category> selectByParentId(Integer parentId);

    /**
     * 获取所有根分类（父分类ID为0或null的分类）
     * @return 根分类列表
     */
    @Select("SELECT * FROM category WHERE parent_id IS NULL OR parent_id = 0 ORDER BY sort")
    List<Category> selectRootCategories();

    /**
     * 检查是否有子分类
     * @param categoryId 分类ID
     * @return 子分类数量
     */
    @Select("SELECT COUNT(*) FROM category WHERE parent_id = #{categoryId}")
    int countChildren(Integer categoryId);
} 